<template>
    <div class="cmdcfg">
        <exInput v-model.number="waitTime" label="等待(秒)" width="100" size="mini"></exInput>
        <span v-if="curtime">时间: {{curtime}}</span>
    </div>
</template>


<script>

export default {
    props: {
        cmdInfo: { type: Object, default: {} },
        devInfo:{ type: Object, default: {} },
    },
    data() {
        return {
            waitTime : this.cmdInfo.waitTime || 1,
            timer : null,
            curtime : 0,
        };
    },
    inject: ['doDevCmd','doNextCmd'],
     

    mounted() {
    },

    methods: {
        setByDevInfo(){
            if(this.cmdInfo.waitTime) this.waitTime = this.cmdInfo.waitTime;
        },
        saveCmd(){
            this.cmdInfo.waitTime = this.waitTime;
        },
        initCmd(){
            this.curtime = 0;
        },
        preCheck(){
            if(!this.waitTime){
                this.$message.error("请输入等待时间")
                return false;
            }
            return true;
        },
        runCmd(isOnce){
            if(!this.preCheck()){
                this.curtime = 0;
                this.$emit("stopRun")
                return;
            }

            this.curtimer = setInterval(()=>{
                // console.info("time++")
                this.curtime ++;
            },1000 )
            this.timer = setTimeout(()=>{
                // console.info("time end")
                this.curtime = 0;
                clearInterval(this.curtimer);
                this.$emit("stopRun")
                if(isOnce!==true) this.doNextCmd();
            },this.waitTime*1000 )

        },
        stopCmd(){
            clearInterval(this.curtimer);
            clearTimeout(this.sTimer);
            this.curtime = 0;
            this.$emit("stopRun")
        }
    }
}
</script>

<style lang="less" scoped>
    .cmdcfg{
        display: inline-block;
    }
</style>
